package com.quanxi.qxaimain;

import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.sql.dialect.oracle.visitor.OracleSchemaStatVisitor;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.parser.SQLParserUtils;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.util.JdbcConstants;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import org.junit.jupiter.api.Test;
import org.springframework.util.CollectionUtils;

import java.text.SimpleDateFormat;
import java.util.*;

import static com.alibaba.druid.sql.SQLUtils.toSQLString;

/**
 * @Author lyx
 * @create 2022/10/21 11:06
 */

@Slf4j
public class sql {

 @Test
    void t1(){


     String str = "se*lect from dd 那你 ";

     String substring = str.substring(  str.indexOf('*')+1,str.length()).toUpperCase();
     System.out.println(substring);


//     System.out.println("=="+tableNameBySql);
//
//
//     Date date = new Date();
//     SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//     System.out.println(simpleDateFormat.format(date));

 }

    public  static List<String> getTableNameBySql(String sql) {
     //String dbType = JdbcConstants.MySql; // mysql
        String dbType = JdbcConstants.ORACLE; // ORACLE
        try {
            List<String> tableNameList = new ArrayList<>();
            //格式化输出
            String sqlResult = SQLUtils.format(sql, dbType);
            log.info("格式化后的sql:[{}]",sqlResult);

            List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
            if (CollectionUtils.isEmpty(stmtList)) {
                log.info("stmtList为空无需获取");
                return Collections.emptyList();
            }
            for (SQLStatement sqlStatement : stmtList) {
              //  MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();// mysql
                OracleSchemaStatVisitor oracleSchemaStatVisitor = new OracleSchemaStatVisitor();// ORACLE
                sqlStatement.accept(oracleSchemaStatVisitor);
                Map<TableStat.Name, TableStat> tables = oracleSchemaStatVisitor.getTables();
                log.info("druid解析sql的结果集:[{}]",tables);
                Set<TableStat.Name> tableNameSet = tables.keySet();
                for (TableStat.Name name : tableNameSet) {
                    String tableName = name.getName();
                    if (StringUtils.isNotBlank(tableName)) {
                        tableNameList.add(tableName);
                    }
                }
            }
            log.info("解析sql后的表名:[{}]",tableNameList);
            return tableNameList;
        } catch (Exception e) {
            log.info("**************异常SQL:[{}]*****************\\n",sql);
            log.info(e.getMessage(),e);
        }
        return Collections.emptyList();
    }





    public  static String geshihua(String sql) {
        //String dbType = JdbcConstants.MySql; // mysql
        String dbType = JdbcConstants.ORACLE; // ORACLE
        try {
            List<String> tableNameList = new ArrayList<>();
            //格式化输出
            String sqlResult = SQLUtils.format(sql, dbType);
            log.info("格式化后的sql:[{}]",sqlResult);
            return sqlResult;
        } catch (Exception e) {
            log.info("**************异常SQL:[{}]*****************\\n",sql);
            log.info(e.getMessage(),e);
            return e.getMessage();
        }
    }


    public static String sqlFormat(String sql) {
        List<SQLStatement> statementList = null;
        SQLStatementParser parser = null;
        try {
            parser = SQLParserUtils.createSQLStatementParser(sql, "oracle");
            statementList = parser.parseStatementList();
        } catch (ParserException e) {
            System.out.println("SQL转换中发生了错误：" + e.getMessage());
            throw e;
        }
        return toSQLString(statementList, "oracle");
    }


}


